/*
* File: 		ring_queue.c
* Author: 		Akira
* Date: 		2023-11-16
* Description:  循环队列的链式存储实现
* Version: 		1.0.0
* Modify: 		None.
*/

#include "ring_queue.h"

void InitRingQueue(LinkRingQueue* rq)
{
    rq->rear = (LinkNode*)malloc(sizeof(LinkNode));
    rq->rear->next = rq->rear;

    rq->length = 0;
}

bool RingQueueEmpty(LinkRingQueue* rq)
{
    return (rq->rear->next->next == rq->rear->next);
}

bool EnRingQueue(LinkRingQueue* rq, ElemType x)
{
    LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
    s->data = x;
    s->next = rq->rear->next;
    rq->rear->next = s;
    rq->rear = s;

    rq->length++;
    return true;
}

bool DeRingQueue(LinkRingQueue* rq, ElemType* x)
{
    if(rq->rear->next->next == rq->rear->next) return false;

    LinkNode *p = rq->rear->next->next;
    *x = p->data;

    if(p == rq->rear)
    {
        rq->rear = rq->rear->next;
        rq->rear->next = rq->rear;
    }
    else
    {
        rq->rear->next->next = p->next;
    }
    
    free(p);

    rq->length--;
    return true;
}

int LengthRingQueue(LinkRingQueue* rq)
{
    return (rq->length);
}

bool GetRingLead(LinkRingQueue* rq, ElemType* x)
{
    if(rq->rear->next->next == rq->rear->next) return false;
    *x = rq->rear->next->next->data;
    return true;
}
